﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting.Utilities;

namespace XmlReader
{
    public partial class XmlMsgCount : DevComponents.DotNetBar.Office2007Form
    {
        private DataSet ds = new DataSet();

        //默认称呼
        string strA = "A2";
        string strB = "Ba";

        //统计数据
        int intA = 0;   //a count
        int intB = 0;   //b count

        int intSucess = 0;
        int intFault = 0;

        //按月份统计
        List<string> listKey = new List<string>();

        DateTime date_min = new DateTime();
        DateTime date_max = new DateTime();

        Dictionary<string, int> dicA2 = new Dictionary<string, int>();
        Dictionary<string, int> dicBa = new Dictionary<string, int>();


        public XmlMsgCount()
        {
            InitializeComponent();
            tabB.Visible = false;
            tabC.Visible = false;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                return;
            }

            this.Text += " " + openFileDialog1.FileName;

            string str = UTF8Encoding.Default.GetString(File.ReadAllBytes(openFileDialog1.FileName));

            ds.Clear();
            ds.ReadXml(new System.IO.StringReader(str));

            dgvData.DataSource = ds.Tables[0];

            tabB.Visible = true;
            tabC.Visible = true;
        }

        private void LoadGraphicStyle()
        {
            cmbA2.Items.Clear();
            cmbBa.Items.Clear();
            cmbAll.Items.Clear();

            cmbA2.Items.Add(strA);
            cmbBa.Items.Add(strB);
            cmbAll.Items.Add("All");

            foreach (object obj in Enum.GetValues(typeof(SeriesChartType)))
            {
                cmbA2.Items.Add(obj.ToString());
                cmbBa.Items.Add(obj.ToString());
                cmbAll.Items.Add(obj.ToString());

                DevComponents.DotNetBar.ButtonItem item = new DevComponents.DotNetBar.ButtonItem(obj.ToString(), "  " + obj.ToString() + "  ");
                item.Click += new EventHandler(all_click);
                itemPanel1.Items.Add(item);
            }
            cmbA2.SelectedIndex = 0;
            cmbBa.SelectedIndex = 0;
            cmbAll.SelectedIndex = 0;
        }

        private void CountData()
        {
            //判断数据是否已载入
            if (ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <=0)
            {
                return;
            }

            //清空数据
            intA = 0;
            intB = 0;
            intSucess = 0;
            intFault = 0;
            dicA2.Clear();
            dicBa.Clear();
            listKey.Clear();

            bool bolIsAll = false;

            string year_mon = "";
            string type = "";
            DateTime dt = new DateTime();
            try
            {
                strB = ds.Tables[0].Rows[0]["Address"].ToString();
            }
            catch (Exception ex)
            {
                return;
            }

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                try
                {
                    if (!bolIsAll)
                    {
                        if (ds.Tables[0].Rows[i]["Address"].ToString() != strB &&
                            ds.Tables[0].Rows[i]["Address"].ToString() != "")
                        {
                            strB = "ALL_User";
                            bolIsAll = true;
                        }
                    }

                    //获取统计数据
                    type = ds.Tables[0].Rows[i]["Type"].ToString();
                    dt = DateTime.Parse(ds.Tables[0].Rows[i]["Date"].ToString());
                    year_mon = dt.Year + "年" + dt.Month + "月";

                    if (i == 0)
                    {
                        date_max = dt;
                        date_min = dt;
                    }
                    else
                    {
                        if (dt >= date_max)
                        {
                            date_max = dt;
                        }
                        else
                        {
                            date_min = dt;
                        }
                    }

                    //根据dic情况添加该月月值键
                    if (!dicA2.ContainsKey(year_mon))
                    {
                        listKey.Add(year_mon);
                        dicA2.Add(year_mon, 0);
                        dicBa.Add(year_mon, 0);
                    }

                    //月份当天数据添加
                    if (type == "1")
                    {
                        dicBa[year_mon] = int.Parse(dicBa[year_mon].ToString()) + 1;
                        intB++;
                    }
                    else
                    {
                        dicA2[year_mon] = int.Parse(dicA2[year_mon].ToString()) + 1;
                        intA++;
                    }
                    intSucess++;
                }
                catch (Exception ex)
                {
                    intFault++;
                }
            }

            lbl1.Text = "    success " + intSucess + " record，fault " + intFault + " record!    =========>      A:" +
                intA + " ,B:" + intB;
        }

        private void ViewChart()
        {
            CountData();
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();

            chart1.Series[0].Name = strA;
            chart1.Series[1].Name = strB;

            for (int i = 0; i < listKey.Count; i++)
            {
                chart1.Series[0].Points.AddXY(listKey[i], dicA2[listKey[i]]);
                chart1.Series[1].Points.AddXY(listKey[i], dicBa[listKey[i]]);
            }
        }

        private void ViewList()
        {
            CountData();
            lv1.Items.Clear();
            for (int i = 0; i < listKey.Count; i++)
            {
                ListViewItem item = new ListViewItem(new string[] { strA, listKey[i].ToString(), dicA2[listKey[i]].ToString() });
                lv1.Items.Add(item);
                item = new ListViewItem(new string[] { strB, listKey[i].ToString(), dicBa[listKey[i]].ToString() });
                lv1.Items.Add(item);
            }
        }

        private void b_Click_1(object sender, EventArgs e)
        {
            ViewChart();
            LoadGraphicStyle();
        }

        private void superTabItem1_Click(object sender, EventArgs e)
        {
            ViewList();
        }

        private void ChangeStyle(string styleA, string styleB)
        {
            //判断数据是否已载入
            if (dicA2.Count <= 0 || dicBa.Count <= 0 || listKey.Count <= 0)
            {
                return;
            }

            string strA2 = "";
            string strBa = "";

            strA2 = styleA;
            strBa = styleB;

            //strAll = styleAll;
            //if (strAll != "" && strAll != "ALL")
            //{
            //    strA2 = strAll;
            //    strBa = strAll;
            //}

            try
            {
                if ( strA2 != strA && strA2 != "" && strA2 != "All") 
                {
                    chart1.Series[0].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), strA2);
                }
                if (strBa != strB && strBa != "" && strBa != "All")
                {
                    chart1.Series[1].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), strBa);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("坑爹啊，出错了：" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void all_click(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.ButtonItem item = (DevComponents.DotNetBar.ButtonItem)sender;
            ChangeStyle(item.Name, item.Name);
        }

        private void cmbAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeStyle(cmbAll.SelectedItem.ToString().Trim(), cmbAll.SelectedItem.ToString().Trim());
        }

        private void cmbA2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeStyle(cmbA2.SelectedItem.ToString(), "");
        }

        private void cmbBa_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeStyle("", cmbBa.SelectedItem.ToString());
        }

    }
}
